﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using EpsiRisk.AffichageJeu;
using System.Threading;

namespace EpsiRisk.MoteurJeu
{
    public static class ClassGestionSouris
    {
        /// <summary>
        /// booleen determinant si un drag&drop est en cours
        /// </summary>
        public static bool DragAndDrop = false;

        /// <summary>
        /// booleen permettant de verouiller la souris
        /// </summary>
        public static bool mouseLocked = false;

        /// <summary>
        /// mouseX
        /// </summary>
        public static int x;

        /// <summary>
        /// mouseY
        /// </summary>
        public static int y;

        /// <summary>
        /// dernier etat du bouton gauche de la souris
        /// </summary>
        public static ButtonState DernierEtatBoutonGauche = ButtonState.Released;

        public static void gererBoutonGauche(ButtonState bs, int mouseX, int mouseY)
        {
            if (bs == ButtonState.Released)
            {
                if (DernierEtatBoutonGauche == ButtonState.Pressed && !DragAndDrop)
                {
                    gererClicGauche(mouseX, mouseY);
                }
                if (DernierEtatBoutonGauche == ButtonState.Pressed && DragAndDrop)
                {
                    gererFinDragDrop(mouseX, mouseY);
                    DragAndDrop = false;
                }
            }
            if (bs == ButtonState.Pressed)
            {
                if (DernierEtatBoutonGauche == ButtonState.Pressed && !DragAndDrop && (mouseX != x || mouseY != y))
                {
                    DragAndDrop = true;
                    gererDebutDragDrop(mouseX, mouseY);

                }

            }

            DernierEtatBoutonGauche = bs;
            x = mouseX;
            y = mouseY;
        }

        public static void gererClicGauche(int mouseX, int mouseY)
        {
            
            if (mouseX < 0 || mouseX > 800 || mouseY < 0 || mouseY > 600 || mouseLocked)
            {
                return;
            }
            Console.WriteLine("Clic Gauche en X=" + mouseX + " Y=" + mouseY);
            if (ClassDonneesGlobales.zonePopupMenu.Visible)
                ClassDonneesGlobales.zonePopupMenu.clicXY(mouseX - (int)ClassDonneesGlobales.zonePopupMenu.position.X, mouseY - (int)ClassDonneesGlobales.zonePopupMenu.position.Y);
            else if (ClassDonneesGlobales.zoneNouveauJeu.Visible)
                ClassDonneesGlobales.zoneNouveauJeu.clicXY(mouseX - (int)ClassDonneesGlobales.zoneNouveauJeu.position.X, mouseY - (int)ClassDonneesGlobales.zoneNouveauJeu.position.Y);
            else
            {
                //souris dans la zone de  jeu
                if (mouseX > 0 && mouseX < 800 && mouseY > 0 && mouseY < 500) { ClassDonneesGlobales.zoneJeu.clicXY(mouseX, mouseY); }
                //souris dans la zone menu
                if (mouseX > 700 && mouseX < 800 && mouseY > 500 && mouseY < 600) { ClassDonneesGlobales.zoneMenu.clicXY(mouseX - 700, mouseY - 500); }
                //souris dans la zone contextuelle
                if (mouseX > 100 && mouseX < 700 && mouseY > 520 && mouseY < 600) { ClassDonneesGlobales.zoneContextuelle.clicXY(mouseX - 100, mouseY - 520); }
            }
            ClassSon.playclick();
        }

        public static int xDebutDD;
        public static int yDebutDD;
        const int dragDropSensibilite = 4;

        public static void gererDebutDragDrop(int mouseX, int mouseY)
        {
            Console.WriteLine("Debut de Drag&Drop en X=" + mouseX + " Y=" + mouseY);
            xDebutDD = x;
            yDebutDD = y;
            //TODO
        }

        public static void gererFinDragDrop(int mouseX, int mouseY)
        {
            Console.WriteLine("Fin de Drag&Drop en X=" + mouseX + " Y=" + mouseY);
            if (Math.Abs(xDebutDD - mouseX) + Math.Abs(yDebutDD - mouseY) < dragDropSensibilite)
            {
                Console.WriteLine("Drag & Drop transformé en clic!");
                gererClicGauche(mouseX, mouseY);
            }
            //TODO
        }

        /// <summary>
        /// Verrouille la souris pour un temps donné
        /// </summary>
        /// <param name="timeout">temps de verouillage (en millisecondes)</param>
        public static void lockMouse(int timeout)
        {
            tLockMouse = new Thread(new ParameterizedThreadStart(fLockMouse));
            tLockMouse.Start(timeout);
        }

        private static Thread tLockMouse;

        private static void fLockMouse(object timeout){
            lock (tLockMouse) {
                mouseLocked = true;
                Console.WriteLine("souris verouillee");
                Thread.Sleep((int)timeout);
                mouseLocked = false;
                Console.WriteLine("souris deverouillee");
            }
        }
    }
}
